home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 10 - 1994 / 10.02 Feb 94 / THINK Top 10 / TRegion.h < prev    next >
Encoding:
Text File  |  1995-07-29  |  5.3 KB  |  164 lines  |  [TEXT/KAHL]

  1. // ** TRegion.h **
  2. //
  3. //  TRegion - class wrapper for Quickdraw regions
  4. //
  5. //    Chris Prinos ©1993, Symantec Corp
  6. //
  7. //    TRegion is a class wrapper for the Quickdraw Region 
  8. //type that provides overloaded     operators to simplify //expressions with regions and other Quickdraw entities used 
  9. //    to form regions. 
  10. //
  11. //    TRegions can be used in mixed expressions such as  
  12. //x = a <op> b, where:
  13. //    
  14. //        • x is a TRegion
  15. //        
  16. //        • a and b are one of:    - TRegion
  17. //                        - RgnHandle
  18. //                        - PicHandle
  19. //                        - Rect
  20. //                        - PolyHandle
  21. //                                
  22. //        • <op> is one of:         +   union of a and b
  23. //                        -   difference between a and b (b from a)
  24. //                        ^   XOR of a and b
  25. //                        &   intersection of a and b
  26. //                        |   union of a and b (same as + )
  27. //
  28. //        • operators can be chained, like x = a ^ b | (c-(d & f));
  29. //
  30. //        • operators +=, -=, ^=, |=, &= are also provided and 
  31. //            are more efficient than their expanded versions, 
  32. //            i.e., use a += c instead of a = a + c for 
  33. //            best performance
  34. //
  35. //        • Boolean operators ||, &&, ==, and ! are provided. 
  36. //            If a TRegion is evaluated in the context of a boolean 
  37. //            expression, it is true if the region is nonempty. 
  38. //            a == b is true if and b describe the same region
  39. //
  40. //  TRegions can be constructed in a variety of ways. In all 
  41. //      cases, the constructor takes care of allocating space for 
  42. //  the private RegionData.  The destructor disposes of any 
  43. //  allocated memory for a region. The constructors 
  44. //  can be specified by any of the following:
  45. //
  46. //        • default            - creates a nil region
  47. //        • a RgnHandle            - makes a TRegion with the 
  48. //                                            RgnHandle (doesn't copy)
  49. //        • a PicHandle            - makes a TRegion that surrounds a 
  50. //                                            Macintosh picture
  51. //        • a Rect            - makes a TRegion that surround the Rect
  52. //        • a PolyHandle        - makes a TRegion that surrounds 
  53. //                                            the Polygon
  54. //        • TRegion            - makes a TRegion with another TRegion 
  55. //                                        (copy ctor)
  56. //
  57. //  Conversion operators: TRegion defines several conversion 
  58. //  operators. Constructors provide conversions to the type 
  59. //  TRegion from the following: RgnHandle, PicHandle, Rect, 
  60. //  PolyHandle. In addition, a conversion operator is provided 
  61. //  to convert from TRegion to RgnHandle so that TRegions can 
  62. //  be used anywhere a RgnHandle is called for, and from 
  63. //  TRegion to int so that TRegions can appear in boolean 
  64. //  expressions
  65. //
  66. //    Utility functions. The following utility functions 
  67. //are provided for TRegion:
  68. //
  69. //        • Empty()            - will empty the TRegion
  70. //        • IsEmpty()            - returns true if the specified 
  71. //                                        TRegion is true
  72. //        • Offset()            - same as OffsetRgn(), but it can take 
  73. //                                        a Point parameter
  74. //        • Inset()            - same as InsetRgn(), but it can take 
  75. //                                        a Point parameter 
  76. //        • ContainsRect        - same as RectInRgn()
  77. //        • ContainsPoint        - same as PtInRgn()
  78. //
  79. //    TRegion uses a nested RegionData class to facilitate 
  80. //reference counting. The reference counting eliminates copy 
  81. //and assignment overhead, as well as reducing storage 
  82. //requirements
  83. #pragma once
  84.  
  85. class TRegion
  86. {
  87.  public:
  88.     // constructors (and implied conversion operators)
  89.     TRegion();
  90.     TRegion(const RgnHandle r);        // doesn't copy data
  91.     TRegion(const Rect &r);
  92.     TRegion(const TRegion &r);
  93.     TRegion(short left, short top, short right, short bottom);
  94.     TRegion(const PolyHandle p);
  95.     TRegion(const PicHandle p);    
  96.         
  97.     // destructors
  98.     ~TRegion();
  99.         
  100.     // Utility member functions
  101.     void         Empty();
  102.     Boolean     IsEmpty() const;
  103.     void         Offset(short h, short v);
  104.     void         Offset(Point p);
  105.     void            Inset(short h, short v);
  106.     void            Inset(Point p);
  107.     Boolean    ContainsRect(const Rect &r) const;
  108.     Boolean     ContainsPoint(Point p) const;
  109.         
  110.     // member function operator overloads
  111.     TRegion&     operator=(const TRegion &r);    // assignment
  112.         
  113.     operator RgnHandle()                    // conversion to RgnHandle
  114.           { return fRgn->fRgnH; }            
  115.     operator int()                                          // Boolean conversion
  116.           { return !EmptyRgn(fRgn->fRgnH); } // true if nonempty
  117.                                                   
  118.     TRegion&    operator+=(const TRegion &r);
  119.     TRegion&    operator-=(const TRegion &r);
  120.     TRegion&    operator^=(const TRegion &r);
  121.     TRegion&    operator|=(const TRegion &r);
  122.     TRegion&    operator&=(const TRegion &r);
  123.     Boolean    operator!()
  124.           { return EmptyRgn(fRgn->fRgnH); }                
  125.         
  126.     // non-member operator overloads
  127.     // these operators need to be friends since they access 
  128.     // private members they are defined as non-member functions 
  129.     // for mixed type expressions
  130.     friend TRegion operator+(const TRegion &a, 
  131.         const TRegion &b);
  132.     friend TRegion operator-(const TRegion &a, 
  133.         const TRegion &b); 
  134.     friend TRegion operator&(const TRegion &a, 
  135.         const TRegion &b);
  136.     friend TRegion operator|(const TRegion &a, 
  137.         const TRegion &b);
  138.     friend TRegion operator^(const TRegion &a, 
  139.         const TRegion &b);
  140.     friend Boolean operator==(const TRegion &a, 
  141.         const TRegion &b);
  142.     friend Boolean operator&&(const TRegion &a, 
  143.         const TRegion &b);
  144.     friend Boolean operator||(const TRegion &a, 
  145.         const TRegion &b);
  146.         
  147.  private:
  148.     struct RegionData                    // nested data class
  149.     {
  150.         RgnHandle    fRgnH;                // Mac Region handle
  151.         short            fCount;            // reference count
  152.             
  153.         RegionData()
  154.             { fCount = 1; fRgnH = NewRgn(); }
  155.         RegionData(RgnHandle r)
  156.             { fCount = 1; fRgnH = r; }
  157.         ~RegionData()
  158.             { DisposeRgn (fRgnH); }
  159.     };            
  160.     RegionData        *fRgn;
  161.  
  162.     void         DetachSharedRgn(); // disconnect from shared RegionData object
  163. };
  164.